﻿using System.Linq;
using Sail.Commands;
using Sail.Localization.Services;

namespace Sail.Localization.Commands
{
    public class CultureCommands : DefaultSailCommandHandler
    {
        private readonly ICultureManager _cultureManager;
        private readonly ISailServices _sailServices;

        public CultureCommands(ICultureManager cultureManager, ISailServices sailServices)
        {
            _cultureManager = cultureManager;
            _sailServices = sailServices;
        }

        [CommandHelp("cultures list \r\n\t" + "List site cultures")]
        [CommandName("cultures list")]
        public void ListCultures()
        {
            Context.Output.WriteLine(T("Listing Cultures:"));

            string cultures = _cultureManager.ListCultures().Aggregate<string, string>(null,
                                                                                       (current, culture) =>
                                                                                       current + culture + " ");

            Context.Output.WriteLine(cultures);
        }


        [CommandHelp("cultures get site culture \r\n\t" + "Get culture for the site")]
        [CommandName("cultures get site culture")]
        public void GetSiteCulture()
        {
            Context.Output.WriteLine(T("Site Culture is {0}", _sailServices.WorkContext.CurrentSite.SiteCulture));
        }

        [CommandHelp("cultures set site culture <culture-name> \r\n\t" + "Set culture for the site")]
        [CommandName("cultures set site culture")]
        public void SetSiteCulture(string cultureName)
        {
            Context.Output.WriteLine(T("Setting site culture to {0}", cultureName));

            if (!_cultureManager.IsValidCulture(cultureName))
            {
                Context.Output.WriteLine(T("Supplied culture name {0} is not valid.", cultureName));
                return;
            }
            _sailServices.WorkContext.CurrentSite.SiteCulture = cultureName;

            Context.Output.WriteLine(T("Site culture set to {0} successfully", cultureName));
        }
    }
}